require("cloud/app.js");
// Use AV.Cloud.define to define as many cloud functions as you want.
// For example:
//修改用户信息
AV.Cloud.define("updateUser", function(req, res) {
	if(req.params.wealthId != undefined){
		var wealth = AV.Object.createWithoutData("UserWealth", req.params.wealthId);
		if(req.params.currency != undefined){
			wealth.set("currency", parseInt(req.params.currency));
		}
		if(req.params.experience != undefined){
			wealth.set("experience", parseInt(req.params.experience));
		}		
		if(req.params.score != undefined){
			wealth.set("score", parseInt(req.params.score));
		}							
		wealth.save(null, {
		  success: function(wealth) {
			console.log('updateUser wealth update success: %j', wealth);
		  },
		  error: function(wealth, error) {
			console.log('wealth false: %j', error.code +" : "+ error.message);
			return res.error();
		  }
		});
	}
	if(req.params.userId != undefined){
		var user = AV.Object.createWithoutData("User", req.params.userId);
		if(req.params.username != undefined){
			user.set("username", req.params.username);
		}
		if(req.params.password != undefined){
			user.set("password", req.params.password);
		}		
		if(req.params.email != undefined){
			user.set("email", req.params.email);	
		}			
		if(req.params.usertype != undefined){
			user.set("usertype", req.params.usertype);	
		}		
		if(req.params.usable != undefined){
			user.set("usable", req.params.usable);
		}		
		if(req.params.nickname != undefined){
			user.set("nickname", req.params.nickname);
		}
		if(req.params.realname != undefined){
			user.set("realname", req.params.realname);
		}		
		if(req.params.phone != undefined){
			user.set("phone", req.params.phone);
		}		
		if(req.params.signature != undefined){
			user.set("signature", req.params.signature);
		}			
		if(req.params.school != undefined){
			user.set("school", req.params.school);
		}
		if(req.params.newPhotoFileId != undefined){
		
			var file = AV.Object.extend("_File");
			var query = new AV.Query(file);
			query.get(req.params.newPhotoFileId, {
			  success: function(file) {
			  	user.set("photoFile", file);
				user.save(null, {
				  success: function(user) {
					if(req.params.oldPhotoFileId != undefined){
						var oleFile = new AV.File();
						oleFile.id = req.params.oldPhotoFileId;
						oleFile.destroy();
						console.log('updateUser delete oldFile: %j', 'success');
					}
					return res.success();
				  },
				  error: function(user, error) {
					console.log('User updateUser  destroy : %j', 'error');
					return res.error();
				  }
				});	
			  },
			  error: function(object, error) {
				console.log('User updateUser newPhotoFile error: %j', error.message);	
			  }
			});							
							
		} else {
			user.save(null, {
			  success: function(user) {
				return res.success();
			  },
			  error: function(user, error) {
				return res.error();
			  }
			});	
		}
		
		
	}
  
});

//删除用户的同时删除用户的头像和用户的订单
AV.Cloud.afterDelete('User', function(request,response) {
  var userObject = AV.Object.createWithoutData('User', request.object.id);
		userObject.fetch({
		success: function(userObject) {
			var wealth = userObject.get("wealth");
			var photoFile = userObject.get("photoFile");				
			if(wealth!= undefined){
				wealth.destroy();
				console.log('User afterDelete  wealth: %j', 'success');	
			}
			if(photoFile!= undefined){
				photoFile.destroy();
				console.log('User afterDelete  photoFile: %j', 'success');		
			}

		}
	});
	
	var relationQuery = new AV.Query("Relation");
	relationQuery.equalTo("userObject", userObject);
	relationQuery.find({
    success: function(relations) {
		AV.Object.destroyAll(relations);
    },
    error: function(error) {
      console.error("Error finding related comments " + error.code + ": " + error.message);
    }
  });
});

//删除任务的同时删除任务的图片和任务订单
AV.Cloud.afterDelete('TaskObject', function(request,response) {
  var taskObject = AV.Object.createWithoutData('TaskObject', request.object.id);
		taskObject.fetch({
		success: function(taskObject) {
			var taskFile = taskObject.get("taskFile");					
			if(taskFile!= undefined){
				taskFile.destroy();
				console.log('TaskObject afterDelete  taskFile : %j', "success");
			}

		}
	});
	var relationQuery = new AV.Query("Relation");
	relationQuery.equalTo("taskObject", taskObject);
	relationQuery.find({
    success: function(relations) {
		AV.Object.destroyAll(relations);
    },
    error: function(error) {
      console.error("Error finding related comments " + error.code + ": " + error.message);
    }
  });
});

//定时修改引流型任务的状态
AV.Cloud.define("task_timer", function(req, res){
	
	var Relation = AV.Object.extend("Relation");
	var query = new AV.Query(Relation);
	query.equalTo("taskFollowTypeCode", "001");
	query.equalTo("taskLimited", "1");
	query.find({
	  success: function(results) {
		for (var i = 0; i < results.length; i++) {
		  var object = results[i];
		  object.set("status", "0");
		  object.save();
		}
	  },
	  error: function(error) {

	  }
	});
    return res.success();
});
